package com.example.web.websystem.response;

/**
 * 常用返回数据类型
 *
 * Created by YangGang on 2019/3/29 14:53。
 */
public class CommonReturnData {
    private Object data;
    private String code;
    private String message;

    /**
     * 自定义所有字段
     *
     * @param data
     * @param code
     * @param message
     * @return
     */
    public static CommonReturnData create(Object data, String code, String message) {
        return new CommonReturnData(data, code, message);
    }

    public static CommonReturnData create(Object data, GlobalCodeEnum globalCodeEnum) {
        return new CommonReturnData(data, globalCodeEnum.getCode(), globalCodeEnum.getMessage());
    }

    /**
     * 成功时：通过数据data构建返回对象
     *
     * @param data
     * @return
     */
    public static CommonReturnData success(Object data) {
        return create(data, GlobalCodeEnum.DEFAULT_SUCCESS);
    }

    public static CommonReturnData success() {
        return create(null, GlobalCodeEnum.DEFAULT_SUCCESS);
    }

    /**
     * 有异常时：通过业务异常创建返回对象
     *
     * @param businessError
     * @return
     */
    public static CommonReturnData error(BusinessError businessError) {
        return create(null, businessError.getGlobalCodeEnum());
    }

    /**
     * 有异常时：自定义msg说明
     *
     * @param msg
     * @return
     */
    public static CommonReturnData error(String msg) {
        return create(null, GlobalCodeEnum.DEFAULT_ERROR.getCode(), msg);
    }

    /**
     * 默认的异常对象
     *
     * @return
     */
    public static CommonReturnData error() {
        return create(null, GlobalCodeEnum.ERROR);
    }

    private CommonReturnData(Object data, String code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
